package com.jz.opengl;

import android.content.Context;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import static android.opengl.GLES10.glClearColor;
import static android.opengl.GLES20.glUseProgram;

/**
 * @author jackzhous
 * @package com.jz.opengl
 * @filename GLRender
 * date on 2019/6/27 3:01 PM
 * @describe TODO
 * @email jackzhouyu@foxmail.com
 **/
public class GLRender implements GLSurfaceView.Renderer {
    private int glProgram;
    private Context context;
    //图像顶点坐标 顶点坐标系是常规的x-y轴，屏幕边界值范围-1，1，屏幕中心是原点
    private float[] vertexData = new float[]{
            //三角形
            -0.5f,      0,
            0.5f,   0,
            0,      1,

            //歪三角形
            -0.5f,      0,
            0.5f,   0,
            1, -1,

            //线
            -0.5f , 0f,
            0.5f , 0f,

            //点
            0, 0.5f

    };

    private FloatBuffer floatBuffer;
    private int vertexGLPos;        //点着色器顶点坐标
    private int fragGLColor;        //片元着色器颜色值


    public GLRender(Context context) {
        this.context = context;
        init();
    }




    private void init(){
        floatBuffer = ByteBuffer.allocateDirect(vertexData.length * 4)
                                .order(ByteOrder.nativeOrder())
                                .asFloatBuffer();
        floatBuffer.put(vertexData);
        floatBuffer.position(0);
    }

    //surfaceView创建时调用
    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        GLES20.glClearColor(0f, 0f, 0f, 0f);
        int vertexShader;
        int fragShader;
        String vertexSource = RawHelper.readRawFile(context, R.raw.vertex_shader);
        String fragSource = RawHelper.readRawFile(context, R.raw.fragment_shader);

        LogHelper.log_i(vertexSource);
        vertexShader = GLHelper.getCompileShader(GLES20.GL_VERTEX_SHADER, vertexSource);
        fragShader = GLHelper.getCompileShader(GLES20.GL_FRAGMENT_SHADER, fragSource);


        if((glProgram = GLHelper.linkOpenGLProgram(vertexShader, fragShader)) == 0){
            LogHelper.show(context, "link error");
            return;
        }
        GLES20.glUseProgram(glProgram);
        vertexGLPos = GLES20.glGetAttribLocation(glProgram, "a_Position");
        fragGLColor = GLES20.glGetUniformLocation(glProgram, "u_Color");
        //2表示向量读取2个单位，即只有X和Y轴 GLES20.GL_FLOAT是类型 false：该值只有在用int型值的时候为true
        floatBuffer.position(0);
        GLES20.glVertexAttribPointer(vertexGLPos, 2, GLES20.GL_FLOAT, false, 0, floatBuffer);
        GLES20.glEnableVertexAttribArray(vertexGLPos);
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        GLES20.glViewport(0, 0, width, height);
    }

    //surfaceView绘制时回调
    @Override
    public void onDrawFrame(GL10 gl) {
        LogHelper.log_i("onDrawFrame");
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
        GLES20.glUniform4f(fragGLColor, 1.0f, 1.0f, 1.0f, 1.0f);
        GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 6);         //绘制三角形  三角形坐标是我们定义数组的0~6位置
        //绘制线
        GLES20.glUniform4f(fragGLColor, 1.0f, 0.0f, 0.0f, 1.0f);
        GLES20.glDrawArrays(GLES20.GL_LINES, 6, 2);
        //绘制点 glUniform4f uniform是不可变类型变量  4f 参数有4个浮点型，该函数主要用于CPU向GPU发送4个颜色值RGBA
        GLES20.glUniform4f(fragGLColor, 0f, 0f, 0f, 0f);
        GLES20.glDrawArrays(GLES20.GL_POINTS, 8, 1);
//      GLES20.glDisableVertexAttribArray(vertexGLPos);
    }
}
